Array.prototype.first = function () {
	return this[0];
};
const fruits = ["apple", "pear", "banana", "orange", "mango"];
fruits.describe = "my favorite fruits";

// 普通for
for (let i = 0; i < fruits.length; i++) {
	console.log(fruits[i]);
}

console.log("----------");

// forEach
fruits.forEach((fruit) => {
	if (fruit === "apple") {
		console.log(fruit);
	}
	// console.log(fruit);
});

console.log("----------");

// forin
for (const key in fruits) {
	if (fruits.hasOwnProperty(key)) {
		const element = fruits[key];
		console.log(element);
	}
}

console.log("----------");

// forof
for (const iterator of fruits.entries()) {
	/* if (iterator === 'orange') {
        continue;
    } */
	console.log(iterator[1]);
}

console.log("----------");

function sum() {
	let total = 0;
	for (const num of arguments) {
		total += num;
	}
	console.log(total);
	return total;
}

sum(10, 25, 313, 423, 512, 20);

console.log("----------");

const name = "laravist";
for (const char of name) {
	console.log(char);
}

console.log("----------");

const lis = document.querySelectorAll("li");
for (const li of lis) {
	li.addEventListener("click", function () {
		this.classList.toggle("completed");
	});
}

console.log("----------");

// Array.from()
// const li = lis.map(todo => todo.textContext); // lis.map is not a function
console.log(lis);
const lisArr = Array.from(lis);
const li = lisArr.map(todo => todo.textContent);
console.log(li);

const liName = Array.from(lis, todo => todo.textContent);
console.log(liName);

function cal() {
	return Array.from(arguments).reduce((previous, current) => previous + current, 0);
}
cal(1, 2, 3, 4, 5);

const website = "laravist";
console.log(Array.from(website));

console.log("----------");

// Array.of()
console.log(Array.of(1, 2, ...website));

console.log("----------");

const inventory = [
	{ name: "apples", quartity: 2 },
	{ name: "oranges", quartity: 0 },
	{ name: "bananas", quartity: 5 },
];
// .find()
/* const bananas = inventory.find(fruit => {
    console.log(fruit);
    if (fruit.name === 'bananas') {
        return true
    }
    return false
}) */
const bananas = inventory.find(fruit => fruit.name === "bananas");
console.log(bananas);

// .findIndex()

const bananasIndex = inventory.findIndex(fruit => fruit.name === "bananas");
console.log(bananasIndex);

// .some()
const isEnough = inventory.some(fruit => fruit.quartity > 2);
console.log(isEnough); // true

// .every()
const allEnough = inventory.every(fruit => fruit.quartity > 2);
console.log(allEnough); // false
